{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# OS ： 操作系统相关\n",
    "- os.getcwd() : 获取当前目录\n",
    "- os.chdir('..') : 改变当前目录\n",
    "- os.mkdir('power') : 创建一个目录\n",
    "- os.remove('') : 删除一个目录\n",
    "- os.removedirs : 递归删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\\python文件\nD:\\python文件\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[WinError 2] 系统找不到指定的文件。: 'test.txt'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-d53b673279e0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetcwd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m# os.mkdir('test.txt')\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'test.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [WinError 2] 系统找不到指定的文件。: 'test.txt'"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "import os\n",
    "print(os.getcwd())\n",
    "os.chdir('D:\\python文件')\n",
    "print(os.getcwd())\n",
    "# os.mkdir('test.txt')\n",
    "os.remove('test.txt')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## path相关的函数\n",
    "- os.getenv() : 获取当前系统变量\n",
    "- os.putenv() : 设置系统变量\n",
    "- os.sep : 系统分隔符，因为系统移植性，最好不要直接打出来 win:\\    unix:/\n",
    "- os.linesep: 换行符   win:\\r\\n   unix: \\n\n",
    "- os.curdir  : 当前目录\n",
    "- os.pardir : 父目录\n",
    "- os.name : 当前系统的系统名    eindows: nt    unix,linux,mac:posix\n",
    "- 注意：有些并不是函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".\n..\nC:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;D:\\phpstudy\\PHPTutorial\\php\\php-7.0.12-nts;C:\\ProgramData\\ComposerSetup\\bin;D:\\应用程序\\python\\Scripts\\;D:\\应用程序\\python\\;C:\\Users\\power\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\power\\AppData\\Roaming\\Composer\\vendor\\bin\n\\\n\r\n\nnt\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "print(os.curdir)\n",
    "print(os.pardir)\n",
    "print(os.getenv('path'))\n",
    "print(os.sep)\n",
    "print(os.linesep)\n",
    "print(os.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\\python文件\npython文件\n路径1\\路径2\\文件3\n路径1\\路径2\n文件3\nFalse\nFalse\n"
     ]
    }
   ],
   "source": [
    "# os.path 模块\n",
    "# 打印出当前路径的绝对路径\n",
    "print(os.path.abspath('.'))\n",
    "# 打印出当前路径的文件部分\n",
    "print(os.path.basename(os.path.abspath('.')))\n",
    "# 使用os.path.json 来拼接文件路径，具有可移植性\n",
    "a = '路径1'\n",
    "b = '路径2\\文件3'\n",
    "x =  os.path.join(a,b)\n",
    "print(x)\n",
    "# 路径分离，用两个变量接受，分别时 目录  和  文件\n",
    "c,d=os.path.split(x)\n",
    "print(c)\n",
    "print(d)\n",
    "# 判断是否是目录\n",
    "print(os.path.isdir(c))\n",
    "print(os.path.isdir(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# shutil 模块\n",
    "- shutil.copy(元地址，目标地址) ：复制，同时也可以改名\n",
    "- shutil.copyfile(源地址，目标地址) ： 复制文件内容\n",
    "- shutil.move(源地址，目标地址) : 移动文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 归档和压缩\n",
    "- 归档就是把多个文件放在一个文件中\n",
    "- 压缩是把多个文件和文件夹有损或者无损的压缩成一个文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# random模块\n",
    "- 随机数\n",
    "- 这里的随机是伪随机的\n",
    "- random.random():随机生成一个0-1的小数\n",
    "- random.randint(0,100) :随机生成一个0-100的整数，包括0-100\n",
    "- random.choice(l) : 随机获取列表l中的一个元素\n",
    "- random.shuffle(l) : 将列表l打乱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.09746573267535596\n14\n[1, 2, 3, 4, 5, 6, 7, 8, 9]\n3\n[9, 7, 6, 2, 8, 4, 5, 3, 1]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "print(random.random())\n",
    "print(random.randint(0,100))\n",
    "l = [i for i in range(1,10)]\n",
    "print(l)\n",
    "print(random.choice(l))\n",
    "random.shuffle(l)\n",
    "print(l)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
